package com.me.study.jdk8.functionInterface;

import org.junit.Test;

import java.util.function.Supplier;

/**
 * JDK 内置的函数式接口--生产型：Supplier接口<p>
 *     特点：只出不进，作为方法/构造参数、方法返回值
 *
 * @author ME
 * @date   2019/12/17
 */
public class TestSupplier {

    @Test
    public void testGet1() {
        System.out.println("\ntestGet1():");

        Supplier<String> supplier = () -> "hhh";
        System.out.println(supplier.get());
    }

    @Test
    public void testGet2() {
        System.out.println("\ntestGet2():");

        int[] arr = {-1, 0, 1, 2, 3};
        int maxValue = getMaxNum(() -> {
            int max = arr[0];
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] > max) {
                    max = arr[i];
                }
            }
            return max;
        });
        System.out.println("数组元素的最大值是：" + maxValue);
    }

    public static int getMaxNum(Supplier<Integer> supplier) {
        return supplier.get();
    }
}
